package cn.topcodes.tcsf.admin.service.impl;

import cn.topcodes.tcsf.admin.commons.generic.GenericService;
import cn.topcodes.tcsf.admin.domain.entity.SysDept;
import cn.topcodes.tcsf.admin.domain.form.NestableSort;
import cn.topcodes.tcsf.admin.service.org.SysDeptService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly=true)
public class SysDeptServiceImpl extends GenericService<SysDept> implements SysDeptService {

    @Override
    @Transactional(readOnly = false)
    public void saveSort(List<NestableSort> sort) {
        tranSaveSort(sort,null);
    }

    @Transactional(readOnly = false)
    private void tranSaveSort(List<NestableSort> sort,Long parentId) {
        if(sort == null) return;
        for(int i = 0 ; i < sort.size() ; i ++ ) {
            NestableSort s = sort.get(i);
            SysDept dept = this.findOne(s.getId());
            dept.setSort(i);
            dept.setParentId(parentId);
            this.update(dept);
            tranSaveSort(s.getChildren(),s.getId());
        }
    }
}
